var t = require("../../../../@babel/runtime/helpers/defineProperty"), a = getApp();

Page({
    data: {
        language: "",
        store: "",
        store_id: "",
        commodity_data: [],
        goods_length: "",
        type: "",
        isLoadMore: !0,
        page: 1,
        total_page: 0,
        goods_audit: 0,
        goods_name: ""
    },
    onLoad: function(t) {
        t.store && this.setData({
            store: t.store,
            type: t.store
        }), t.store_id && this.setData({
            store_id: t.store_id
        });
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {
        this.initData(), this._getJson(), "store" == this.data.store && this.getGoodsAaudit();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.data.isLoadMore ? this.getMoreData() : console.log("没有更多数据");
    },
    onShareAppMessage: function() {},
    _getJson: function() {
        var t = this, o = "", e = {};
        "store" == this.data.store ? (o = "plugin.store-cashier.frontend.store.goods.get-goods-list", 
        e = {
            store_id: this.data.store_id
        }) : "supplier" == this.data.store && (o = "plugin.supplier.frontend.goods.get-goods-list", 
        e = {
            sid: this.data.store_id
        });
        var s = a.getNetAddresss(o);
        a._postNetWork({
            url: s,
            showToastIn: !1,
            data: e,
            success: function(o) {
                var e = o.data;
                if (1 == e.result) {
                    if (t.setData({
                        commodity_data: []
                    }), "store" == t.data.store) {
                        for (var s = 0; s < e.data.goods_data.length; s++) e.data.goods_data[s].edit_active = !1;
                        console.log(e.data, "数组的合并"), t.setData({
                            goods_length: e.data.count,
                            total_page: e.data.goods_data.last_page,
                            commodity_data: e.data.goods_data.data
                        }), "https://dev3.yunzshop.com/" == a.data.host && t.setData({
                            commodity_data: e.data.goods_data
                        });
                    } else if ("supplier" == t.data.store) {
                        for (var d = 0; d < e.data.list.length; d++) e.data.list[d].edit_active = !1;
                        console.log(e.data, "数组的格式"), t.setData({
                            total_page: e.data.list.last_page,
                            goods_length: e.data.list.data.length,
                            commodity_data: e.data.list.data
                        });
                    }
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {}
        });
    },
    moreEvent: function(a) {
        var o = a.currentTarget.dataset.index;
        0 == this.data.commodity_data[o].edit_active ? (this.emptyEditActive(), this.setData(t({}, "commodity_data[" + o + "].edit_active", !0))) : this.emptyEditActive();
    },
    emptyEditActive: function() {
        for (var a = 0; a < this.data.commodity_data.length; a++) this.setData(t({}, "commodity_data[" + a + "].edit_active", !1));
    },
    routeTake: function() {
        wx.navigateTo({
            url: "/packageA/member/supplier/CommodityRelease/CommodityRelease?store_id=" + this.data.store_id + "&store=" + this.data.store
        }), this.emptyEditActive();
    },
    commodityDelete: function(t) {
        var o = this, e = t.currentTarget.dataset.index;
        this.emptyEditActive(), wx.showModal({
            title: "提示",
            content: "此操作将永久删除该商品, 是否继续?",
            success: function(t) {
                if (t.confirm) {
                    var s = "", d = {};
                    "store" == o.data.store ? (s = a.getNetAddresss("plugin.store-cashier.frontend.store.goods.destroy-goods"), 
                    d = {
                        goods_id: o.data.commodity_data[e].id
                    }) : "supplier" == o.data.store && (s = a.getNetAddresss("plugin.supplier.frontend.goods.del-goods"), 
                    d = {
                        goods_id: o.data.commodity_data[e].id
                    }), a._postNetWork({
                        url: s,
                        showToastIn: !1,
                        data: d,
                        success: function(t) {
                            var a = t.data;
                            if (1 == a.result) {
                                wx.showToast({
                                    icon: "none",
                                    title: "商品删除成功",
                                    duration: 1500
                                });
                                var s = o.data.commodity_data;
                                s.splice(e, 1), o.setData({
                                    commodity_data: s,
                                    goods_length: Number(o.data.goods_length) - 1
                                });
                            } else wx.showToast({
                                icon: "none",
                                title: a.msg,
                                duration: 1500
                            });
                        },
                        fail: function(t) {}
                    });
                } else t.cancel && wx.showToast({
                    icon: "none",
                    title: "已取消删除",
                    duration: 1500
                });
            }
        });
    },
    editJumpEvent: function(t) {
        var a = t.currentTarget.dataset.index;
        console.log(this.data.commodity_data[a].id, a, "11111222"), wx.navigateTo({
            url: "/packageA/member/supplier/CommodityRelease/CommodityRelease?store_id=" + this.data.store_id + "&goods_id=" + this.data.commodity_data[a].id + "&store=" + this.data.store
        });
    },
    initData: function() {
        this.setData({
            commodity_data: [],
            goods_length: "",
            isLoadMore: !0,
            page: 1,
            total_page: 0,
            goods_audit: 0
        });
    },
    changeGoodsStatus: function(o) {
        var e = o.currentTarget.dataset.item, s = o.currentTarget.dataset.index;
        if ("supplier" !== this.data.store) {
            var d = 0;
            1 !== e.status && (d = 1);
            var i = this, n = a.getNetAddresss("plugin.store-cashier.frontend.store.goods.changeGoodsStatus");
            a._getNetWork({
                url: n,
                data: {
                    goods_id: e.id,
                    status: d
                },
                success: function(a) {
                    1 == a.data.result ? 1 === d ? (i.setData(t({}, "commodity_data[" + s + "].status", 1)), 
                    wx.showToast({
                        title: "上架成功",
                        icon: "none",
                        duration: 1500
                    })) : (i.setData(t({}, "commodity_data[" + s + "].status", 0)), wx.showToast({
                        title: "下架成功",
                        icon: "none",
                        duration: 1500
                    })) : 1 === d ? wx.showToast({
                        title: "上架失败",
                        icon: "none",
                        duration: 1500
                    }) : wx.showToast({
                        title: "下架失败",
                        icon: "none",
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    getMoreData: function() {
        var t = this, o = "", e = {};
        "store" == t.data.store ? (o = a.getNetAddresss("plugin.store-cashier.frontend.store.goods.get-goods-list"), 
        e = {
            store_id: t.data.store_id
        }) : "supplier" == t.data.store && (o = a.getNetAddresss("plugin.supplier.frontend.goods.get-goods-list"), 
        e = {
            sid: t.data.store_id
        }), t.setData({
            isLoadMore: !1
        }), this.data.page >= this.data.total_page || (t.setData({
            page: this.data.page + 1
        }), e.page = t.data.page, a._getNetWork({
            url: o,
            data: e,
            success: function(a) {
                var o = a.data;
                if (1 == o.result) {
                    if ("store" == t.data.store) {
                        for (var e = 0; e < o.data.goods_data.data.length; e++) o.data.goods_data.data[e].edit_active = !1;
                        t.setData({
                            commodity_data: t.data.commodity_data.concat(o.data.goods_data.data)
                        });
                    } else if ("supplier" == t.data.store) {
                        for (var s = 0; s < o.data.list.data.length; s++) o.data.list.data[s].edit_active = !1;
                        t.setData({
                            commodity_data: t.data.commodity_data.concat(o.data.list.data)
                        });
                    }
                    t.setData({
                        isLoadMore: !0
                    });
                } else t.setData({
                    page: t.data.page - 1,
                    isLoadMore: !1
                });
            },
            fail: function(t) {
                console.log(t);
            }
        }));
    },
    onCancel: function() {
        this.setData({
            goods_name: ""
        });
    },
    onChange: function(t) {
        console.log(t), this.setData({
            goods_name: t.detail
        });
    },
    onSearch: function() {
        var t = this, o = "", e = {};
        "supplier" == this.data.store ? (o = "plugin.supplier.frontend.goods.get-goods-list", 
        e = {
            goods_name: this.data.goods_name,
            sid: this.data.store_id
        }) : (o = "plugin.store-cashier.frontend.store.goods.get-goods-list", e = {
            goods_name: this.data.goods_name,
            store_id: this.data.store_id
        });
        var s = a.getNetAddresss(o);
        a._getNetWork({
            url: s,
            data: e,
            success: function(a) {
                console.log(a), 1 == a.data.result ? "supplier" == t.data.store ? t.setData({
                    commodity_data: a.data.data.list.data,
                    page: a.data.data.list.current_page,
                    total: a.data.data.list.last_page
                }) : t.setData({
                    commodity_data: a.data.data.goods_data.data,
                    page: a.data.data.goods_data.current_page,
                    total: a.data.data.goods_data.last_page
                }) : wx.showToast({
                    icon: "none",
                    title: a.data.msg
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getGoodsAaudit: function() {
        var t = this, o = a.getNetAddresss("plugin.store-cashier.frontend.store.setting.get-goods-audit");
        a._getNetWork({
            url: o,
            data: {},
            success: function(a) {
                var o = a.data;
                1 == o.result && t.setData({
                    goods_audit: o.data.goods_audit
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    }
});